#! /bin/sh
# PCP QA Test No. 628
# libpcp_pmda botch in pmdaFetch for multiple DSO PMDAs used in the
# on pmFetch ... bug #809111
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -d $PCP_PMDAS_DIR/simple ] || _notrun "simple PMDA directory is not installed"

status=1	# failure is the default!

_cleanup()
{
    cd $here

    [ -f $tmp.root ] && $sudo cp $tmp.root $PCP_VAR_DIR/pmns/root
    $sudo rm -f $PCP_VAR_DIR/pmns/idiot
    [ -f $tmp.root.bin ] && $sudo cp $tmp.root.bin $PCP_VAR_DIR/pmns/root.bin
    [ -f $tmp.pmcd.conf ] && $sudo cp $tmp.pmcd.conf $PCP_PMCDCONF_PATH
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    $sudo rm -rf $tmp.* $PCP_PMDAS_DIR/idiot
    exit $status
}

trap "_cleanup" 0 1 2 3 15

home=$PCP_PMDAS_DIR
cd $home/simple

# restart pmcd and copy the pmcd config file and pmns to restore state later
#
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1
cp $PCP_PMCDCONF_PATH $tmp.pmcd.conf
cp $PCP_VAR_DIR/pmns/root $tmp.root
[ -f $PCP_VAR_DIR/pmns/root.bin ] && cp $PCP_VAR_DIR/pmns/root.bin $tmp.root.bin

if $sudo $PCP_MAKE_PROG clobber >$tmp.out 2>&1
then
    :
else
    cat $tmp.out
    echo "Arrgh, make clobber failed"
    exit
fi

$sudo rm -rf $PCP_PMDAS_DIR/idiot
$sudo mkdir $PCP_PMDAS_DIR/idiot
$sudo chmod 777 $PCP_PMDAS_DIR/idiot
cp * $PCP_PMDAS_DIR/idiot

$sudo ./Remove >/dev/null 2>&1
cat <<End-of-File | $sudo ./Install >$tmp.out 2>&1
dso
End-of-File

echo "=== simple install ===" >>$seq_full
cat $tmp.out >>$seq_full

if pminfo -v simple
then
    :
else
    echo "simple DSO install failed! ... here is the Install log ..."
    cat $tmp.out
fi

cd $PCP_PMDAS_DIR/idiot
mv simple.c idiot.c
mv simple.conf idiot.conf
for suffix in pl py perl python
do
    [ -f pmdasimple.$suffix ] && mv pmdasimple.$suffix pmdaidiot.$suffix
done

for file in *
do
    sed <$file >$tmp.tmp \
	-e '/simple/s//idiot/g' \
	-e '/SIMPLE/s//IDIOT/g' \
	-e '/253/s//177/g'
    chmod u+w $file
    cp $tmp.tmp $file
done
for file in pmns help
do
    sed <$file >$tmp.tmp -e '/IDIOT/s//177/g'
    cp $tmp.tmp $file
done

$sudo ./Remove >/dev/null 2>&1
cat <<End-of-File | $sudo ./Install >$tmp.out 2>&1
dso
End-of-File

echo "=== idiot install ===" >>$seq_full
cat $tmp.out >>$seq_full

if pminfo -v idiot
then
    :
else
    echo "idiot DSO install failed! ... here is the Install log ..."
    cat $tmp.out
fi


# real QA test starts here
pminfo -fm simple.numfetch | tee $tmp.check

if false
then
    # enable this trap to debug the infamous e_ext_t bug
    #
    if grep 'No value' $tmp.check >/dev/null 2>&1
    then
	echo "Bad pminfo trap (No values for simple.numfetch)"
	echo "Continue?"
	read ans </dev/tty
    fi
fi

echo "=== pmcd log after first pminfo simple ===" >>$seq_full
cat $PCP_LOG_DIR/pmcd/pmcd.log >>$seq_full
pminfo -fm idiot.numfetch
pminfo -fm idiot.numfetch
pminfo -fm idiot.numfetch
pminfo -fm simple.numfetch idiot.numfetch
echo "=== pmcd log after last pminfo simple ===" >>$seq_full
cat $PCP_LOG_DIR/pmcd/pmcd.log >>$seq_full

# success, all done
status=0
exit
